package cn.xx.org.controller;

import cn.xx.basic.util.AjaxResult;
import cn.xx.basic.util.PageList;
import cn.xx.org.domain.Department;
import cn.xx.org.query.DepartmentQuery;
import cn.xx.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
@Api(tags = "部门接口",description = "部门接口详细描述")
//查询单个对象接口：get localhost:8080/department/1
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    // 查询单个对象： get localhost:8080/department/1
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个对象",notes = "根据id查询")
    public Department findById(@PathVariable("id") Long id) {
        return departmentService.findById(id);
    }

    //查询所有接口：get localhost:8080/department
    @ApiOperation(value = "查询所有",notes = "")
    @GetMapping()
    public List<Department> findAll() {
        return departmentService.findAll();
    }

    //加载部门树
    @ApiOperation(value = "加载部门树",notes = "")
    @GetMapping("/deptTree")
    public List<Department> getDeptTree() {
        return departmentService.findDeptTree();
    }

    //添加和修改接口 Put localhost:8080/department
    //从请求体中获取数据：@RequestBody
    @PutMapping
    @ApiOperation(value = "部门添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult saveOrUpdate(@RequestBody Department department) {
        try {
            if (department.getId() == null) {
                departmentService.save(department);
            } else {
                departmentService.update(department);
            }
            return AjaxResult.me().setSuccess(true).setMsg("操作完成了哦");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败").setResultObject("原因:" + e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除",notes = "根据id删除")
    //删除接口：Delete localhost:8080/department/1
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            departmentService.delete(id);
            return AjaxResult.me().setSuccess(true).setMsg("操作完成了哦");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败").setResultObject("原因:" + e.getMessage());
        }
    }

    //分页查询接口
    @PostMapping
    @ApiOperation(value = "分页查询",notes = "")
    public PageList<Department> pageQuery(@RequestBody DepartmentQuery departmentQuery) {
        return departmentService.pageQuery(departmentQuery);
    }




    // 批量删除
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids ) {
        try {
            departmentService.patchDel(ids);
            return AjaxResult.me().setSuccess(true).setMsg("操作完成了哦");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败").setResultObject("原因:" + e.getMessage());
        }
    }
}
